package exercise3;

public class Sort {

    public void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    public void mergeSort(int[] array) {
        mergeFunc(array, 0, array.length - 1);
    }

    public void shallSort(int[] array) {
        int gap = array.length / 2;
        while (gap >= 1) {
            shall(array,gap);
            gap /= 2;
        }
    }

    public void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array,0,end);
            end--;
            siftDown(array,0,end);
        }
    }

    private void createBigHeap(int[] array) {
        for (int parent = (array.length-1-1) / 2; parent >= 0; parent--) {
            siftDown(array,parent,array.length - 1);
        }
    }

    private void siftDown(int[] array, int parent, int end) {
        int child = parent * 2 + 1;
        while (child <= end) {
            if (child + 1 <= end && array[child] < array[child+1]) {
                child = child + 1;
            }
            if (array[parent] < array[child]) {
                swap(array,parent,child);
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }

    private void shall(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    private void mergeFunc(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int midIndex = (end - start) / 2 + start;

        mergeFunc(array, start, midIndex);
        mergeFunc(array, midIndex + 1, end);

        merge(array, start, midIndex, end);
    }

    private void merge(int[] array, int left, int mid, int right) {
        int[] tmpArray = new int[right - left + 1];
        int i = 0;
        int sr = mid + 1;
        int tmp = left;
        while (left <= mid && sr <= right) {
            if (array[left] < array[sr]) {
                tmpArray[i++] = array[left++];
            } else {
                tmpArray[i++] = array[sr++];
            }
        }
        while (left <= mid) {
            tmpArray[i++] = array[left++];
        }
        while (sr <= right) {
            tmpArray[i++] = array[sr++];
        }
        for (int j = tmp; j <= right; j++) {
            array[j] = tmpArray[j - tmp];
        }
    }

    private void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        // 使用插入排序对最后一层进行优化
        if (end - start + 1 < 15) {
            insertSortWithQuick(array, start, end);
            return;
        }

        // 三数取中
        int midIndex = midNumber(array, start, end);
        swap(array, midIndex, start);

        int part = partition2(array, start, end);

        quick(array, start, part - 1);
        quick(array, part + 1, end);
    }

    private void insertSortWithQuick(int[] array, int start, int end) {
        for (int i = start; i <= end; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    private int midNumber(int[] array, int start, int end) {
        int midIndex = (end - start) / 2 + start;
        if (array[midIndex] > array[start]) {
            if (array[end] > array[midIndex]) {
                return midIndex;
            } else if (array[end] < array[start]) {
                return start;
            } else {
                return end;
            }
        } else {
            if (array[end] > array[start]) {
                return start;
            } else if (array[end] < array[midIndex]) {
                return midIndex;
            } else {
                return end;
            }
        }
    }

    private int partition1(int[] array, int left, int right) {
        int tmp = array[left];
        int retIndex = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, retIndex, left);
        return left;
    }

    private int partition2(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    private void swap(int[] array, int x, int y) {
        int ret = array[x];
        array[x] = array[y];
        array[y] = ret;
    }
}
